//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DiscrepancyStatistics.h"
using namespace Cephei::QL::Math::Statistics;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (UInt64 dimension) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDiscrepancyStatistics = NULL;
#endif
        QuantLib::Size _dimension = (QuantLib::Size)ValueHelper::Convert (dimension);
        _ppDiscrepancyStatistics = new boost::shared_ptr<QuantLib::DiscrepancyStatistics> (new QuantLib::DiscrepancyStatistics ( _dimension ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (boost::shared_ptr<QuantLib::DiscrepancyStatistics>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscrepancyStatistics = NULL;
#endif
	_ppDiscrepancyStatistics = &childNative;
    
}
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (QuantLib::DiscrepancyStatistics& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDiscrepancyStatistics = NULL;
#endif
	_ppDiscrepancyStatistics = new boost::shared_ptr<QuantLib::DiscrepancyStatistics> (&childNative);
    
    _DiscrepancyStatisticsOwner = owner;
    
}

Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (CDiscrepancyStatistics^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscrepancyStatistics = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscrepancyStatistics = new boost::shared_ptr<QuantLib::DiscrepancyStatistics> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscrepancyStatistics = NULL;
#endif
	if (!t->IsSubclassOf(CDiscrepancyStatistics::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (QuantLib::Handle<QuantLib::DiscrepancyStatistics>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDiscrepancyStatistics = &childNative;
	_ppDiscrepancyStatistics = &static_cast<boost::shared_ptr<QuantLib::DiscrepancyStatistics>>(childNative.currentLink());
    
    _DiscrepancyStatisticsOwner = owner;
}
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (QuantLib::Handle<QuantLib::DiscrepancyStatistics> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDiscrepancyStatistics = &childNative;
	_ppDiscrepancyStatistics = &static_cast<boost::shared_ptr<QuantLib::DiscrepancyStatistics>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::CDiscrepancyStatistics (QuantLib::DiscrepancyStatistics childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscrepancyStatistics = NULL;
#endif
	_ppDiscrepancyStatistics = new boost::shared_ptr<QuantLib::DiscrepancyStatistics> (new QuantLib::DiscrepancyStatistics (childNative));
    
}
#endif

Cephei::QL::Math::Statistics::CDiscrepancyStatistics::~CDiscrepancyStatistics ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDiscrepancyStatistics != NULL)
    {
	    delete _ppDiscrepancyStatistics;
        _ppDiscrepancyStatistics = NULL;
    }
}
Cephei::QL::Math::Statistics::CDiscrepancyStatistics::!CDiscrepancyStatistics ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDiscrepancyStatistics != NULL)
    {
	    delete _ppDiscrepancyStatistics;
    }
}
QuantLib::DiscrepancyStatistics& Cephei::QL::Math::Statistics::CDiscrepancyStatistics::GetReference ()
{
    if (_ppDiscrepancyStatistics == NULL) throw gcnew NativeNullException ();
	return **_ppDiscrepancyStatistics;
}
boost::shared_ptr<QuantLib::DiscrepancyStatistics>& Cephei::QL::Math::Statistics::CDiscrepancyStatistics::GetShared ()
{
    if (_ppDiscrepancyStatistics == NULL) throw gcnew NativeNullException ();
	return *_ppDiscrepancyStatistics;
}
QuantLib::DiscrepancyStatistics* Cephei::QL::Math::Statistics::CDiscrepancyStatistics::GetPointer ()
{
    if (_ppDiscrepancyStatistics == NULL) throw gcnew NativeNullException ();
	return &**_ppDiscrepancyStatistics;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscrepancyStatistics>& Cephei::QL::Math::Statistics::CDiscrepancyStatistics::GetHandle ()
{
	if (_phDiscrepancyStatistics == NULL)
	{
		_phDiscrepancyStatistics = new Handle<QuantLib::DiscrepancyStatistics> (*_ppDiscrepancyStatistics);
	}
	return *_phDiscrepancyStatistics;
}
#endif
bool Cephei::QL::Math::Statistics::CDiscrepancyStatistics::HasNative () 
{
	return (_ppDiscrepancyStatistics != NULL);
}

Double Cephei::QL::Math::Statistics::CDiscrepancyStatistics::Discrepancy::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDiscrepancyStatistics)->discrepancy ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::IDiscrepancyStatistics^ Cephei::QL::Math::Statistics::CDiscrepancyStatistics::Reset (Microsoft::FSharp::Core::FSharpOption<UInt64>^ dimension)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _dimension = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (dimension) ? (QuantLib::Size)ValueHelper::Convert (dimension->Value) : 0); //9a
    	(*_ppDiscrepancyStatistics)->reset ( _dimension );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Statistics::IDiscrepancyStatistics^ Cephei::QL::Math::Statistics::CDiscrepancyStatistics_Factory::Create (UInt64 dimension)
{
    return gcnew CDiscrepancyStatistics ( dimension);
}
